package com.myhome.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myhome.domain.CashRecharge;
import com.myhome.dto.UserDto;
import com.myhome.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myhome.mapper.CoinRechargeMapper;
import com.myhome.domain.CoinRecharge;
import com.myhome.service.CoinRechargeService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
*@Author QYiXin 
* @Date 2021 10 20 09 37
**/
@Service
public class CoinRechargeServiceImpl extends ServiceImpl<CoinRechargeMapper, CoinRecharge> implements CoinRechargeService{
    @Autowired
    UserFeign userFeign;
    @Override
    public Page<CoinRecharge> getPageCashRecharge(Page<CoinRecharge> page, Long coinId, Long userId, String userName, String mobile, Byte status, String numMin, String numMax, String startTime, String endTime) {
        LambdaQueryWrapper<CoinRecharge> lambdaQuery = new LambdaQueryWrapper<>();
        Map<Long, UserDto> userDtoMap = null;
        if (!StringUtils.isEmpty(userId) || !StringUtils.isEmpty(userName) || !StringUtils.isEmpty(mobile)) {
            userDtoMap = userFeign.getMapUserDto(Arrays.asList(userId), userName, mobile);
            if (ObjectUtils.isEmpty(userDtoMap)) {
                return page;
            }
            lambdaQuery.in(CoinRecharge::getUserId, userDtoMap.keySet());
        }
        lambdaQuery.eq(coinId!=null, CoinRecharge::getCoinId, coinId)
                .eq(status!=null,CoinRecharge::getStatus, status)
                .between((!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)), CoinRecharge::getCreated, startTime, endTime + " 23:59:59")
                .between((!StringUtils.isEmpty(numMax) && !StringUtils.isEmpty(numMin)), CoinRecharge::getAmount, new BigDecimal(numMin == null ? "0" : numMin), new BigDecimal(numMax == null ? "0" : numMax));
        Page<CoinRecharge> result = this.page(page, lambdaQuery);
        List<CoinRecharge> records = result.getRecords();
        System.out.println("结果："+userDtoMap==null);
        if(userDtoMap==null){ // 说明前面没有使用用户的信息查询用户
            List<Long> userIds = records.stream().map(cashRecharge -> cashRecharge.getUserId()).collect(Collectors.toList());
            if (!CollectionUtil.isEmpty(userIds)) {
                userDtoMap =  userFeign.getMapUserDto(userIds,null,null) ;
            }
        }
        if (records != null) {
            for (CoinRecharge record : records) {
                if (userDtoMap != null) {
                    UserDto userDto = userDtoMap.get(record.getUserId());
                    if (userDto != null) {
                        record.setUsername(userDto.getUsername());
                        record.setRealName(userDto.getRealName());
                    }
                }
            }
        }
        return result;
    }

    @Override
    public Page<CoinRecharge> findUserCoinRecharge(Page<CoinRecharge> page, Long coinId, Long userId) {
        return page(page,new LambdaQueryWrapper<CoinRecharge>()
                .eq(coinId!=null,CoinRecharge::getCoinId,coinId)
                .eq(CoinRecharge::getUserId ,userId)
        );
    }
}
